const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');

const writeStream = fs.createWriteStream('test.csv');

// 写入csv首行信息
writeStream.write('时间,标题,网址 \n');

const url = 'http://www.xbmu.edu.cn/frontChannelPage.action?siteId=1&articleClassId=104';
axios.get(url)
    .then(function (response) {
        // handle success
        // console.log(response);
        // 使用cheerio分析信息
        const $ = cheerio.load(response.data);
        // 使用CSS选择符选择内容
        // console.log($('title').text());
        // console.log($('#container div.list_right_main > ul > li').text());
        // $('#container div.list_right_main > ul > li a').each((index, element) => {
        //     const newsDate = $(element).prev().text();
        //     const item = $(element).text();
        //     const link = $(element).attr('href');
        //     console.log(newsDate, item, link);
        // });
        // $('#container div.list_right_main li').each((index, element) => {
        //     const newsDate = $(element).find('span').text();
        //     const item = $(element).find('a').text();
        //     const link = $(element).find('a').attr('href');
        //     console.log(newsDate, item, link);
        //     writeStream.write(`${newsDate}, ${item},${link} \n`);
        // });
        // console.log($('#nav-menu > ul > li').text());
    })
    .catch(function (error) {
        // handle error
        console.log(error);
    });
